using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml;

namespace Omniture.API
{
	public enum TrackItemTypeConstants
	{
		PropItem,
		EvarItem,
		EventItem,
	}

	public class TrackItemCollection : List<ITrackItem>, IXmlable
	{
		#region Class Members

		private OmnitureProject _parent = null;
		private TrackItemTypeConstants _collectionType = 0;

		#endregion

		#region Constructors

		internal TrackItemCollection(OmnitureProject parent, TrackItemTypeConstants collectionType)
		{
			_parent = parent;
			_collectionType = collectionType;
		}

		#endregion

		#region Property Implementations

		public OmnitureProject ParentItem
		{
			get { return _parent; }
		}

		#endregion

		#region Methods

		public new void Add(ITrackItem item)
		{
			if (item.Id == 0)
			{
				this.ParentItem.LastObjectIndex++;
				item.Id = this.ParentItem.LastObjectIndex;
			}
			base.Add(item);
		}

		public ITrackItem NewItem()
		{
			this.ParentItem.LastObjectIndex++;
			ITrackItem retval;
			if (_collectionType == TrackItemTypeConstants.EvarItem)
				retval = new EVarItem(this, this.ParentItem.LastObjectIndex);
			else if (_collectionType == TrackItemTypeConstants.EventItem)
				retval = new EventItem(this, this.ParentItem.LastObjectIndex);
			else if (_collectionType == TrackItemTypeConstants.PropItem)
				retval = new PropItem(this, this.ParentItem.LastObjectIndex);
			else
				throw new Exception("The type was not found!");

			this.Add(retval);
			return retval;
		}

		public ITrackItem this[string name]
		{
			get
			{
				foreach (ITrackItem c in this)
				{
					if (string.Compare(c.Name, name, true) == 0)
						return c;
				}
				return null;
			}
		}

		public ITrackItem GetById(int id)
		{
			foreach (ITrackItem c in this)
			{
				if (c.Id == id)
					return c;
			}
			return null;
		}

		public bool Contains(string name)
		{
			foreach (ITrackItem t in this)
			{
				if (string.Compare(t.Name, name, true) == 0)
					return true;
			}
			return false;
		}

		public bool Contains(int index)
		{
			return (0 <= index) && (index < this.Count);
		}

		public bool ContainsId(int columnId)
		{
			foreach (ITrackItem t in this)
			{
				if (t.Id == columnId)
					return true;
			}
			return false;
		}

		public int IndexOf(string name)
		{

			for (int ii = 0; ii < this.Count; ii++)
			{
				if (this[ii].Name == name)
					return ii;
			}
			return -1;
		}

		public void Remove(string name)
		{
			ITrackItem removeItem = null;
			foreach (ITrackItem t in this)
			{
				if (string.Compare(t.Name, name, true) == 0)
					removeItem = t;
			}

			if (removeItem != null)
				this.Remove(removeItem);
		}

		public int GetNextUniqueId()
		{
			int retval = 1;
			foreach (ITrackItem item in this)
			{
				if (retval <= item.Id)
					retval = item.Id + 1;
			}
			return retval;
		}

		#endregion

		#region IXmlable Members

		public virtual void LoadXml(XmlNode parentNode)
		{
			this.Clear();
			_collectionType = (TrackItemTypeConstants)Enum.Parse(typeof(TrackItemTypeConstants), XmlHelper.GetAttribute(parentNode, "CollectionType", _collectionType.ToString("d")));
			foreach (XmlNode node in parentNode.ChildNodes)
			{
				ITrackItem newItem = null;
				if (_collectionType == TrackItemTypeConstants.EvarItem)
					newItem = new EVarItem(this.ParentItem.EVarList, 0);
				else if (_collectionType == TrackItemTypeConstants.EventItem)
					newItem = new EventItem(this.ParentItem.EventList, 0);
				else if (_collectionType == TrackItemTypeConstants.PropItem)
					newItem = new PropItem(this.ParentItem.PropList, 0);
				else
					throw new Exception("Collection type not found!");

				newItem.LoadXml(node);
				this.Add(newItem);
			}
		}

		public virtual void SaveXml(XmlNode parentNode)
		{
			//Now loop and add children
			foreach (ITrackItem t in this)
			{
				XmlNode newNode = XmlHelper.AddNode(parentNode, "Item");
				t.SaveXml(newNode);
			}
		}

		#endregion

	}
}
